Skip to content

Conversation

@HaohaiWen
Copy link
Contributor

@HaohaiWen HaohaiWen commented Aug 1, 2025

Use std::optional<std::string> as return type of getReproduceOption
instead of const char *. This is same as getReproduceFile in COFF which
allows linker to modify reproduce tar name.

Use std::optional<std::string> as return type of getReproduceOption
instead of const char *. This is same as getReproduceFile in COFF which
allows user to modify reproduce tar name.
@llvmbot
Copy link
Member

llvmbot commented Aug 1, 2025

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Haohai Wen (HaohaiWen)

Changes

Use std::optional<std::string> as return type of getReproduceOption
instead of const char *. This is same as getReproduceFile in COFF which
allows user to modify reproduce tar name.


Full diff: https://github.com/llvm/llvm-project/pull/151656.diff

1 Files Affected:

  • (modified) lld/ELF/Driver.cpp (+8-4)
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6c2f318ffe469..1a33fdc21b25c 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -482,10 +482,14 @@ static void checkOptions(Ctx &ctx) {
     ErrAlways(ctx) << "-z force-ibt may not be used with -z retpolineplt";
 }
 
-static const char *getReproduceOption(opt::InputArgList &args) {
+static std::optional<std::string> getReproduceOption(opt::InputArgList &args) {
   if (auto *arg = args.getLastArg(OPT_reproduce))
     return arg->getValue();
-  return getenv("LLD_REPRODUCE");
+
+  if (const char *env = getenv("LLD_REPRODUCE"))
+    return env;
+
+  return std::nullopt;
 }
 
 static bool hasZOption(opt::InputArgList &args, StringRef key) {
@@ -681,11 +685,11 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
     Msg(ctx) << getLLDVersion() << " (compatible with GNU linkers)";
 
-  if (const char *path = getReproduceOption(args)) {
+  if (std::optional<std::string> path = getReproduceOption(args)) {
     // Note that --reproduce is a debug option so you can ignore it
     // if you are trying to understand the whole picture of the code.
     Expected<std::unique_ptr<TarWriter>> errOrWriter =
-        TarWriter::create(path, path::stem(path));
+        TarWriter::create(*path, path::stem(*path));
     if (errOrWriter) {
       ctx.tar = std::move(*errOrWriter);
       ctx.tar->append("response.txt", createResponseFile(args));

@HaohaiWen HaohaiWen requested review from MaskRay and dcci August 1, 2025 06:31
@MaskRay
Copy link
Member

MaskRay commented Aug 1, 2025

I don't understand the change. The ELF port already respects LLD_REPRODUCE

@HaohaiWen
Copy link
Contributor Author

HaohaiWen commented Aug 1, 2025

I don't understand the change. The ELF port already respects LLD_REPRODUCE

We'd like linker to auto create tar name based on output instead of manually specify a different reproduce flag every time. That requires to store its temporary name. (e.g. when user exports LLD_REPRODUCE_DIR, lld will create {output}.tar in this folder) I don't know whether community is interested in that.

I know this PR looks weird for unnecessary copy. If that is not acceptable, I can defer upstreaming this change until we really need to create a temporary name.

@HaohaiWen HaohaiWen closed this Aug 1, 2025
@HaohaiWen
Copy link
Contributor Author

Let's defer it until real temporary name storage is required.

@HaohaiWen HaohaiWen deleted the repro branch August 1, 2025 07:16
@MaskRay
Copy link
Member

MaskRay commented Aug 1, 2025

I don't think the proposed LLD_REPRODUCE_DIR is necessary. You can always a wrapper to add a --reproduce= option. The current reproduce file behavior is simple and easy to explain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants